//
//  UserInfo.m
//  CustomerServer
//
//  Created by 涂俊 on 2019/4/13.
//  Copyright © 2019年 Facebook. All rights reserved.
//

#import "UserInfo.h"


#import "InitManager.h"

@implementation UserInfo

- (id)init {
  if (self = [super init]) {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imLoginSuccess) name:@"im_login_ok" object:nil];
  }
  return self;
}

-(NSDictionary *) getInfo {
    ArgumentStore *store = ArgumentStore.current;
    NSString *uid = [store uid]?:@"";
  NSString *roleId = [store roleId]?:@"";
  NSString *roleName = [store roleName]?:@"";
  NSString *appId = [store appId]?:@"";
  NSString *pubKey = [store pubKey]?:@"";
  NSString *log = [store log]?:@"";
  NSString *url = [store baseUrl]?:@"";
  
  NSString* serverName = [store serverName]?:@"";
  NSString* rom = [store rom]?:@"";
  NSString* ram = [store ram]?:@"";
  NSString* os = [store os]?:@"";
  NSString* gameName = [store gameName]?:@"";
  NSString* gameVersion = [store gameVersion]?:@"";
  NSString* channelId = [store channelId]?:@"";
  NSString* sdkId = [store sdkId]?:@"";
  NSString* network = [store network]?:@"";
  NSString* configInit = [store configInit]?:@"";
  if (!configInit) {
    configInit = @"";
  }
  NSString* loginResult = [ArgumentStore.current imLoginResult];
  if (!loginResult) {
    loginResult = @"fail";
  }
  return @{@"uid":uid,
           @"role_id":roleId,
           @"role_name":roleName,
           @"KEFU_APP_ID":appId,
           @"KEFU_PUBLIC_KEY":pubKey,
           @"KEFU_LOG":log,
           @"KEFU_URL":url,
           @"server_name":serverName,
           @"rom":rom,
           @"ram":ram,
           @"os":os,
           @"game_name":gameName,
           @"game_version":gameVersion,
           @"channel_id":channelId,
           @"sdk_id":sdkId,
           @"network":network,
           @"init_config":configInit,
           @"login":loginResult,
           };
}

+ (NSDictionary *) getInfo:(ArgumentStore *)store {
    NSString *uid = [store uid]?:@"";
    NSString *roleId = [store roleId]?:@"";
    NSString *roleName = [store roleName]?:@"";
    NSString *appId = [store appId]?:@"";
    NSString *pubKey = [store pubKey]?:@"";
    NSString *log = [store log]?:@"";
    NSString *url = [store baseUrl]?:@"";
    
    NSString* serverName = [store serverName]?:@"";
    NSString* rom = [store rom]?:@"";
    NSString* ram = [store ram]?:@"";
    NSString* os = [store os]?:@"";
    NSString* gameName = [store gameName]?:@"";
    NSString* gameVersion = [store gameVersion]?:@"";
    NSString* channelId = [store channelId]?:@"";
    NSString* sdkId = [store sdkId]?:@"";
    NSString* network = [store network]?:@"";
    NSString* configInit = [store configInit]?:@"";
    if (!configInit) {
        configInit = @"";
    }
    NSString* loginResult = [store imLoginResult]?:@"fail";
    if (!loginResult) {
        loginResult = @"fail";
    }
    return @{@"appUid":uid,
             @"appRoleId":roleId,
             @"roleName":roleName,
             @"appId":appId,
             @"publicKey":pubKey,
//             @"KEFU_LOG":log,
//             @"KEFU_URL":url,
             @"serverName":serverName,
             @"rom":rom,
             @"ram":ram,
             @"os":os,
             @"gameName":gameName,
             @"gameVersion":gameVersion,
             @"channelId":channelId,
             @"sdkId":sdkId,
             @"network":network,
//             @"init_config":configInit,
             @"login":loginResult,
             @"platform": @"IOS",
    };
}

//RCT_REMAP_METHOD(init,
//                 initWithResolver:(RCTPromiseResolveBlock)resolve
//                 rejecter:(RCTPromiseRejectBlock)reject) {
//    ArgumentStore *store = ArgumentStore.current;
//    NSString *uid = [store uid];
//    NSString *roleId = [store roleId];
//    NSString *appId = [store appId];
//    [[InitManager getInstance] initWithAppId:appId uid:uid roleId:roleId callback:^(BOOL result) {
//    if (result) {
//      resolve([self getInfo]);
//    } else {
//      reject(@"fail", @"init fail", nil);
//    }
//  }];
//}

//RCT_REMAP_METHOD(login,
//                 loginWithResolver:(RCTPromiseResolveBlock)resolve
//                 rejecter:(RCTPromiseRejectBlock)reject) {
//    ArgumentStore *store = ArgumentStore.current;
//    NSString *uid = [store uid];
//    NSString *roleId = [store roleId];
//    NSString *appId = [store appId];
//    if ([[InitManager getInstance] loginWithAppId:appId uid:uid roleId:roleId]) {
//        resolve(@"");
//    } else {
//        reject(@"fail", @"login fail", nil);
//    }
//}

//RCT_REMAP_METHOD(getUserInfo,
//                 getUserInfoWithResolver:(RCTPromiseResolveBlock)resolve
//                 rejecter:(RCTPromiseRejectBlock)reject) {
//  resolve([self getInfo]);
//}
//
//// 获取缓存key对应的value
//RCT_REMAP_METHOD(getMetaData,
//                 getMetaDataWithKey:(NSString *)key
//                 resolver:(RCTPromiseResolveBlock)resolve
//                 rejecter:(RCTPromiseRejectBlock)reject) {
//    ArgumentStore *store = ArgumentStore.current;
//    if ([@"KEFU_APP_ID" isEqualToString:key]) {
//        resolve(@{key:[store appId]});
//    } else if ([@"KEFU_PUBLIC_KEY" isEqualToString:key]) {
//        resolve(@{key:[store pubKey]});
//    } else if ([@"KEFU_LOG" isEqualToString:key]) {
//        resolve(@{key:[store log]});
//    } else if ([@"KEFU_URL" isEqualToString:key]) {
//        resolve(@{key:[store baseUrl]});
//    } else if ([@"IM_APP_KEY" isEqualToString:key]) {
//        resolve(@{key:[store imAppKey]});
//    } else if ([@"IM_TENANT_ID" isEqualToString:key]) {
//        resolve(@{key:[store imTenantId]});
//    } else if ([@"IM_SERVICE_NUMBER" isEqualToString:key]) {
//        resolve(@{key:[store conversationId]});
//    } else {
//        reject(@"fail", @"noValue", nil);
//    }
//}
//
//// 获取缓存key对应的value
//RCT_REMAP_METHOD(getValue,
//                 getValueWithKey:(NSString *)key
//                 resolver:(RCTPromiseResolveBlock)resolve
//                 rejecter:(RCTPromiseRejectBlock)reject) {
//    ArgumentStore *store = ArgumentStore.current;
//    NSString *value = [store getCache:key];
//    if (value == nil) {
//        value = @"";
//    }
//    resolve(@{key:value});
//}
//
//// 添加缓存key/value
//RCT_EXPORT_METHOD(putValue:(NSString *)key value:(NSString *)value) {
//    ArgumentStore *store = ArgumentStore.current;
//    [store setCache:value forKey:key];
//}

#pragma mark - RCTEventEmitter 需要实现的方法

- (NSArray<NSString *> *)supportedEvents
{
  return @[@"login"];
}

#pragma mark - 主动通知rn

// 登录成功，主动通知rn层
- (void)imLoginSuccess {
  
}

@end
